package com.infonautics.action;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.hibernate.Assignpurview;
import com.hibernate.Category;
import com.hibernate.Userinfo;
import com.hibernate.Userrole;
import com.infonautics.dao.ICategoryDao;
import com.infonautics.dao.IUserroleDao;
import com.infonautics.dao.imp.ICategoryDaoImp;
import com.infonautics.dao.imp.IUserroleDaoImp;
import com.infonautics.page.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.system.dao.ISystemDao;

public class UserroleAction extends ActionSupport implements Preparable,ServletRequestAware,SessionAware{
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private String pageUrl = "/WEB-INF/userrole/";
	private String successMsg;
	private String url;
	private String errormessage;
	private Userinfo userinfo;
	private ISystemDao<Userrole> userroleDao = new IUserroleDaoImp();
	private IUserroleDao roleDao = new IUserroleDaoImp();
	private ICategoryDao catDao = new ICategoryDaoImp();
	private Userrole userrole;
	private PageBean<Userrole> pageBean;
	private int page = 1;
	private int id;
	private StringBuffer sbpurview=new StringBuffer();	
	private List<Category> categorys;
	private String rolepermission="";
	private String[] perarray;
	public HttpServletRequest request;
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	@Override
	public void prepare() throws Exception {
		if(session.containsKey("userinfo")){
			if(session.get("userinfo")!=null){
				userinfo=(Userinfo)session.get("userinfo");
			}
		}
		
	}
	
	//查询用户角色列表
	public String list() throws Exception{
		pageBean = roleDao.doUserroleList(20, page);
		return "input";
	}
	
	//新增用户角色页面
	public String addPage()throws Exception{
		return "input";
	}
	
	//新增用户角色
	public String add() throws Exception{
		userroleDao.doAdd(userrole);
		doMsgHandle("添加成功","Userrole_list.action");
		return UserroleAction.SUCCESS;
	}
	
	//删除用户角色
	public String delete() throws Exception{
		userroleDao.doDelete(id);
		doMsgHandle("删除成功","Userrole_list.action");
		return UserroleAction.SUCCESS;
	}
	public String purview()throws Exception{//进入权限分配页面
		List<Assignpurview> assignList = roleDao.doAssignList(id);
		getChildList("",1,sbpurview,catDao,assignList);
		return "input";
	}

	private StringBuffer getChildList(String tabString,int ParentID,StringBuffer sb,ICategoryDao catDao,List<Assignpurview> assignList) throws Exception{
		categorys=catDao.doByParentid(ParentID);
		if(categorys!=null&&categorys.size()>0){
			for(Category cat:categorys){
				sb.append("<ul>");
				sb.append("<li style='width:180px;'>");
				sb.append(tabString);
				sb.append("&nbsp;&nbsp;");
				sb.append("<input type='checkbox' name='");
				sb.append("menu");
				sb.append(cat.getId());
				sb.append("' value='");
				sb.append(cat.getId());
				sb.append("' ");
				for(Assignpurview assign:assignList){
					if(cat.getId().equals(assign.getCategory().getId())){
						sb.append(" checked='checked' ");
					}
				}
				sb.append("/>");
				sb.append(cat.getName());
				sb.append("</li>");
				sb.append("\n");
				sb.append("<li>");
				if(cat.getPermissions()!=null){
					Object permis_tmp=roleDao.getValue("select ownpurview from assignpurview where roleid="+id+" and purviewid="+cat.getId()+" and rownum<=1");
					if(permis_tmp!=null&&permis_tmp.toString().length()>0){
						rolepermission=permis_tmp.toString();
					}
					perarray=cat.getPermissions().split("\\|");
					for(String s:perarray){
						sb.append("<input type='checkbox' name='");
						sb.append("menu");
						sb.append(cat.getId());
						sb.append("_permissions");
						sb.append("' value='");
						sb.append(s);
						sb.append("' ");
						if(rolepermission!=null&&rolepermission.indexOf(","+s+",")>-1){
							sb.append("checked='checked'");
						}
						sb.append("/>");
						sb.append(s);
					}
				}
				sb.append("</li>");
				sb.append("\n");
				sb.append("</ul>");
				sb.append("\n");
				getChildList(tabString+"&nbsp;&nbsp;",cat.getId(),sb,catDao,assignList);
			}
		}
		return sb;
	}
	public String assgin() throws Exception{//分配权限
		roleDao.doDeletePurview(id);
		userrole = userroleDao.doOne(id);
		getChildpurview(1);
		doMsgHandle("提交成功","Userrole_list.action");
		return UserroleAction.SUCCESS;
	}
	
	private void getChildpurview(int ParentID) throws Exception{
		categorys=catDao.doByParentid(ParentID);
		if(categorys!=null&&categorys.size()>0){
			for(Category cat:categorys){
				if(request.getParameter("menu"+cat.getId())!=null){
					Assignpurview purview=new Assignpurview();
					purview.setUserrole(userrole);
					purview.setCategory(cat);
					String permission="";
					String[] rolepermission=request.getParameterValues("menu"+cat.getId()+"_permissions");
					if(rolepermission!=null&&rolepermission.length>0){
						for(String s:rolepermission){
							permission+=",";
							permission+=s;
							permission+=",";
							permission+="|";
						}
						permission=permission.substring(0,permission.length()-1);
					}
					purview.setOwnpurview(permission);
					roleDao.assignPurview(purview);
				}
				 getChildpurview(cat.getId());
			}
		}
	}
	//服务方法
	private void doMsgHandle(String successMsg,String url){
		this.successMsg=successMsg;
		this.url=url;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String getErrormessage() {
		return errormessage;
	}
	
	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}
	
	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}
	
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	
	public String getSuccessMsg() {
		return successMsg;
	}
	
	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public Userrole getUserrole() {
		return userrole;
	}

	public void setUserrole(Userrole userrole) {
		this.userrole = userrole;
	}

	public PageBean<Userrole> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<Userrole> pageBean) {
		this.pageBean = pageBean;
	}

	public StringBuffer getSbpurview() {
		return sbpurview;
	}

	public void setSbpurview(StringBuffer sbpurview) {
		this.sbpurview = sbpurview;
	}

	public List<Category> getCategorys() {
		return categorys;
	}

	public void setCategorys(List<Category> categorys) {
		this.categorys = categorys;
	}

	public String getRolepermission() {
		return rolepermission;
	}

	public void setRolepermission(String rolepermission) {
		this.rolepermission = rolepermission;
	}

	public String[] getPerarray() {
		return perarray;
	}

	public void setPerarray(String[] perarray) {
		this.perarray = perarray;
	}

	
	
}
